package com.itggm.service.impl;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itggm.Result.ResponseResult;
import com.itggm.dto.LogsExportDTO;
import com.itggm.dto.LogsPageDTO;
import com.itggm.pojo.LogInfo;
import com.itggm.service.LogInfoService;
import com.itggm.mapper.LogInfoMapper;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.stereotype.Service;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
* @author root
* @description 针对表【sys_log_info】的数据库操作Service实现
* @createDate 2025-01-02 10:02:25
*/
@Service
public class LogInfoServiceImpl extends ServiceImpl<LogInfoMapper, LogInfo> implements LogInfoService{
    @Override
    public void deleteLogs() {
        baseMapper.deleteLogs();
    }

    @Override
    public ResponseResult logsPage(LogsPageDTO logsPageDTO) {
        //创建分页对象
        Page<LogInfo> logInfoPage = new Page<>(logsPageDTO.getPage(), logsPageDTO.getLimit());
        //构造分页条件
        LambdaQueryWrapper<LogInfo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.isNotBlank(logsPageDTO.getUsername()), LogInfo::getUsername, logsPageDTO.getUsername())
                .like(StringUtils.isNotBlank(logsPageDTO.getIp()), LogInfo::getIp, logsPageDTO.getIp())
                .like(StringUtils.isNotBlank(logsPageDTO.getModel()), LogInfo::getModel, logsPageDTO.getModel())
                .orderByDesc(LogInfo::getCreateTime);
        //执行分页
        baseMapper.selectPage(logInfoPage, queryWrapper);
        //返回分页结果
        return ResponseResult.success()
                .data("total", logInfoPage.getTotal())
                .data("tableData", logInfoPage.getRecords());
    }

    @Override
    public void exportLogs(LogsExportDTO logsExportDTO, HttpServletResponse response) {
        String username = logsExportDTO.getUsername();
        String ip = logsExportDTO.getIp();
        String model = logsExportDTO.getModel();


        LambdaQueryWrapper<LogInfo> lambda = new QueryWrapper<LogInfo>().lambda();
        lambda.eq(StringUtils.isNotBlank(ip), LogInfo::getIp, ip)
                .eq(StringUtils.isNotBlank(username), LogInfo::getUsername, username)
                .eq(StringUtils.isNotBlank(model), LogInfo::getModel, model);

        List<LogInfo> logInfos = baseMapper.selectList(lambda);

        ExportParams exportParams = new ExportParams();
        String formatDate = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
        exportParams.setSheetName(formatDate);
        exportParams.setTitle(formatDate+"日志信息");

        Workbook workbook = ExcelExportUtil.exportExcel(exportParams, LogInfo.class, logInfos);
        try {
            ServletOutputStream outputStream = response.getOutputStream();
            workbook.write(outputStream);
            outputStream.close();
            workbook.close();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}




